package com.wangsh.webforward.common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangsh.webforward.common.pojo.ApiResponse;
import com.wangsh.webforward.common.util.ConstatFinalUtil;
import com.wangsh.webforward.common.util.DateFormatUtil;
import com.wangsh.webforward.common.util.FileUtil;
import com.wangsh.webforward.common.util.HttpUtil;
import com.wangsh.webforward.common.util.JsoupUtil;
import com.wangsh.webforward.common.util.POIUtil;

/**
 * 所有Servlet的父类,公共代码
 * 
 * @author TeaBig
 */
public abstract class BaseServlet extends HttpServlet
{
	/* 提示信息 */
	protected String info  ; 
	
	/* httpUtil工具类 */
	protected HttpUtil httpUtil = new HttpUtil();
	/* 日期工具类 */
	protected DateFormatUtil dateFormatUtil = new DateFormatUtil();
	protected POIUtil poiUtil = new POIUtil();
	protected FileUtil fileUtil = new FileUtil();
	protected JsoupUtil jsoupUtil = new JsoupUtil();
	
	/**
	 * 抽象方法,初始化对象
	 */
	public abstract void initObject() ; 
	
	/**
	 * service方法
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.initObject();
	}
	
	/**
	 * 响应数据
	 * @throws IOException 
	 */
	protected void printOut(HttpServletResponse response , String responseStr) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		/* 获取输出流 */
		PrintWriter out = response.getWriter() ; 
		/* 输出数据 */
		out.print(responseStr);
		
		/* 刷新 */
		out.flush();
		/* 关闭 */
		out.close();
	}
	
	/**
	 * 响应数据
	 * @throws IOException 
	 */
	protected void clearInfo(HttpServletRequest request,HttpServletResponse response,
			ApiResponse apiResponse,
			String clearFlagParams) throws IOException
	{
		String blank = ""; 
		/* 存储页面的提示信息 */
		String clearFlagRes = "";
		
		/* 接收参数 */
		String clearReq = request.getParameter("clearFlag") ; 
		if("".equalsIgnoreCase(clearFlagRes) && clearReq != null && !"".equalsIgnoreCase(clearReq))
		{
			clearFlagRes = clearReq ; 
		}
		
		/* 方法参数 */
		if("".equalsIgnoreCase(clearFlagRes) && clearFlagParams != null && !"".equalsIgnoreCase(clearFlagParams))
		{
			clearFlagRes = clearFlagParams ; 
		}
		/* 清除信息 */
		if("true".equalsIgnoreCase(clearFlagRes))
		{
			/* 清除信息 */
			request.setAttribute("response", blank);
			HttpSession session = request.getSession() ; 
			session.setAttribute("response", blank);
			if(apiResponse != null)
			{
				apiResponse.setInfo("", Collections.EMPTY_MAP);
			}
		}else
		{
			/* 保留信息 */
		}
	}
	
	/**
	 * 设置信息
	 */
	protected void setInfo(HttpServletRequest request,ApiResponse apiResponse)
	{
		request.setAttribute("response", apiResponse.toJSON());
		
		HttpSession session = request.getSession() ; 
		session.setAttribute("response", apiResponse.toJSON());
	}
	
	/**
	 * 设置公共的信息
	 */
	protected void commonInfo(HttpServletRequest request)
	{
		/* 获取网址 */
		String webFileUrl = ConstatFinalUtil.CONFIG_JSON.getString("website.fileUrl");
		/* 存储requeset */
		request.setAttribute("webFileUrl", webFileUrl);
		
		/* 获取网址 */
		String usersCenterLogin = ConstatFinalUtil.CONFIG_JSON.getString("usersCenter.head.login.url");
		/* 存储requeset */
		request.setAttribute("usersCenterLogin", usersCenterLogin);
		
		/* 外部请求网址:spliderServer.outer.url */
		String spliderServerOuterUrl  = ConstatFinalUtil.CONFIG_JSON.getString("spliderServer.outer.url ");
		/* 存储requeset */
		request.setAttribute("spliderServerOuterUrl", spliderServerOuterUrl);
	}
	
	/**
	 * 信息页面
	 * @throws IOException 
	 * @throws ServletException 
	 */
	protected boolean infoPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/head/info.jsp"; 
		request.getRequestDispatcher(path).forward(request, response);
		return true;
	}
}
